package com.sdses.weather.converter;

import com.sdses.weather.config.mapping.FieldConverter;
import com.sdses.weather.enums.WeatherCondition;
import org.springframework.stereotype.Component;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component("weatherConditionEnumConverter") // 注意：这个 Bean 名要和 YAML 中 converter 一致
public class WeatherConditionEnumConverter implements FieldConverter<WeatherCondition> {

    // 示例：墨迹天气 condition code 映射表
    private static final Map<String, WeatherCondition> CODE_TO_CONDITION = Collections.unmodifiableMap(
            Stream.of(
                    new AbstractMap.SimpleImmutableEntry<>("100", WeatherCondition.SUNNY),
                    new AbstractMap.SimpleImmutableEntry<>("101", WeatherCondition.CLOUDY),
                    new AbstractMap.SimpleImmutableEntry<>("102", WeatherCondition.OVERCAST),
                    new AbstractMap.SimpleImmutableEntry<>("300", WeatherCondition.RAIN),
                    new AbstractMap.SimpleImmutableEntry<>("301", WeatherCondition.THUNDERSTORM),
                    new AbstractMap.SimpleImmutableEntry<>("400", WeatherCondition.SNOW),
                    new AbstractMap.SimpleImmutableEntry<>("500", WeatherCondition.FOG),
                    new AbstractMap.SimpleImmutableEntry<>("501", WeatherCondition.HAZE),
                    new AbstractMap.SimpleImmutableEntry<>("502", WeatherCondition.SANDSTORM)
                    // 可继续扩展...
            ).collect(Collectors.toMap(
                    Map.Entry::getKey,   // Key mapper
                    Map.Entry::getValue  // Value mapper
            ))
    );

    @Override
    public WeatherCondition convert(Object input, String provider) {
        if (input == null) return WeatherCondition.UNKNOWN;

        String code = input.toString().trim();
        return CODE_TO_CONDITION.getOrDefault(code, WeatherCondition.UNKNOWN);
    }
}